'''
https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/
SO32.py 一个题目只是对输出结果进行了反转操作
'''
from collections import deque
from util.TreeHelper import *
from typing import List

class Solution:
    def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
        res = []
        queue = deque()
        if root:
            queue.append(root)
        while queue:
            tmp = []
            for _ in range(0, len(queue)):
                root = queue.popleft()
                if root:
                    tmp.append(root.val)
                    queue.append(root.left)
                    queue.append(root.right)
            if tmp:
                res.append(tmp)
        res.reverse()
        return res

if __name__ == "__main__":
    
    print(Solution().levelOrderBottom(genTreeFromStr("[3,9,20,null,null,15,7]")))